
package privatemessanger;
import javax.swing.*;
import java.awt.Color;

/**
 * This panel contains a list of the connected server.
 * @author  Erez Fatal, Oren Slor, Elad Yanai, Sigal Agami, Chen Ben Shabat,
 *          Inbar Pardo, Islamm Heralla, Ziv Hadary, Oren Shalev, Ortal David.
 */
public class ServerNamePanel extends JPanel
{
    private MainFrame _frame; // the frame.
    private DefaultListModel _listModel;
    private JList _list;

    /**
     * This is the ServerNamePanel constructor.
     * @param frame The frame which creates this panel.
     */
    ServerNamePanel(MainFrame frame)
    {
        _frame=frame;
        setBackground(Color.LIGHT_GRAY);
        _listModel = new DefaultListModel();

         //Create the list and put it in a scroll pane.
        _list = new JList(_listModel);
        _list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        _list.setSelectedIndex(0);
        _list.addListSelectionListener(_frame);
        _list.setVisibleRowCount(37);
        _list.setFixedCellWidth(175);
        _list.setBackground(new Color(190, 231, 249));
        JScrollPane listScrollPane = new JScrollPane(_list);
        listScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        listScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(listScrollPane);
    }

    /**
     * Get the list.
     * @return the list.
     */
    public JList getList() { return _list; }

    /**
     * This method updates the list of servers.
     */
    public void updateList()
    {
        _listModel.clear();
        for (int i=0; i<_frame.getUser().getServerArray().size(); i++)
            _listModel.addElement(_frame.getUser().getServerArray().get(i).getServerName()+" ("+_frame.getUser().getServerArray().get(i).getIP()+")");
        _frame.getContactName().getListModel().clear();
        _frame.getContactName().cleanSelectedClient();
    }
}
